package Z字形变换;

class Solution {
    public String convert(String s, int numRows) {
        if(numRows == 1){
            return s;
        }
        int d = 2*numRows - 2;
        int n = s.length();
        char[] ret = new char[n];
        int f = 0;
        for(int i = 0; i < n; i += d){
            ret[f] = s.charAt(i);
            f++;
        }
        for(int i = 1; i < numRows - 1; i++){
            for(int j = i, k = d - i; j < n || k < n; j += d,k += d){
                if(j < n){
                    ret[f] = s.charAt(j);
                    f++;
                }
                if(k < n){
                    ret[f] = s.charAt(k);
                    f++;
                }
            }
        }
        for(int i = numRows - 1; i < n; i += d){
            ret[f] = s.charAt(i);
            f++;
        }
        return String.valueOf(ret);
    }
}